﻿using System;
using System.Net;
using System.Windows;
using Hammock;
using Hammock.Web;
using Microsoft.Phone.Controls;

namespace MyMeetup
{
    public partial class MemberPage : PhoneApplicationPage
    {
        private Member _member;
        private string _memberId;
        private string _groupId;
        private bool networkError;

        public MemberPage()
        {
            InitializeComponent();
        }

        protected void NetworkErrorCallback(RestRequest request, RestResponse response, object userState)
        {
            Dispatcher.BeginInvoke(() =>
                {
                    progressBar.IsIndeterminate = false;
                    if (!networkError)
                        MessageBox.Show("There was a problem connecting to meetup.com.",
                                        "Network Error", MessageBoxButton.OK);
                    networkError = true;
                });
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (!NavigationContext.QueryString.TryGetValue("memberId", out _memberId) ||
                !NavigationContext.QueryString.TryGetValue("groupId", out _groupId))
            {
                if (_member == null && State.ContainsKey("CurrentMember"))
                    _member = State["CurrentMember"] as Member;

            }

        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            State["CurrentMember"] = _member;
        }

    }
}